package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void add(SetmealDTO setmealDTO) {
        //添加套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.addSetmeal(setmeal);

        //获取生成的套餐id
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });

        //添加菜品到套餐中
        setmealDishMapper.addSetmealDishes(setmealDishes);
    }

    /**
     * 套餐分页
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //使用PageHelper插件分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //PageHelper要求后面方法返回值固定是page
        Page<SetmealVO>  page= setmealMapper.page(setmealPageQueryDTO);

        //封装PageResult
        Long total = page.getTotal();
        List<SetmealVO> result = page.getResult();
        return new PageResult(total,result);
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void delete(Long[] ids) {
        setmealMapper.deleteByIsd(ids);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {

        //获取套餐基础信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //更新套餐信息
        setmealMapper.updateSetmeal(setmeal);

        //获取套餐菜品信息
        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();

        //更新套餐菜品信息
        //1.先删除套餐菜品信息 2.再添加套餐菜品信息
//        setmealDishMapper.deleteById(setmealDTO.getId());

        setmealDishMapper.getSetmealIdByDishId(setmealDTO.getId());
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
        });
        setmealDishMapper.addSetmealDishes(setmealDishes);
    }

    /**
     * 根据id查套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        //查询套餐基本信息
        Setmeal setmeal = new Setmeal();
        setmeal=setmealMapper.getSetmealById(id);

        //查询套餐菜品信息
        List<SetmealDish> setmealDishes =setmealDishMapper.getSetmealIdByDishId(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 套餐启停
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //当要修改成起售时，判断套餐里是否有菜品处于停售状态。有则报错，无则修改
        if(status == StatusConstant.ENABLE){
            //获取套餐中的菜品信息
            List<Dish> dishes = dishMapper.getSetmeal(id);
            if(dishes != null && dishes.size() > 0){
                dishes.forEach(dish -> {
                    if(StatusConstant.DISABLE == dish.getStatus()){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.updateSetmeal(setmeal);
    }
}
